#include "client.h"

Client::Client(QObject * parent): QTcpSocket(parent)
{
    //readyread()信号继承自QIODevice，当有数据传送过来时，除法该信号
    connect(this, SIGNAL(readyRead()), this, SLOT(dataRecive()));
    //dosconnected信号好像继承自QAbstractSocket
    connect(this, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}

void Client::dataRecive()
{
    //bytesAvailable继承自QIODevice，返回传送过来的数据的字节大小
    while(bytesAvailable() > 0)
    {
        int length = bytesAvailable();
        char buf[1024];
        //read第一个参数是bytesArray
        read(buf, length);
        QString msg = buf;
        //触发updateClients信号
        emit updateClients(msg, length);
    }
}

void Client::slotDisconnected()
{
    emit disconnected(this->socketDescriptor());
}

